package com.youfang.sixthmedia.common;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Environment;

import com.gzit.store.StoreManager;
import com.gzit.utils.AndroidUtils;
import com.gzit.utils.StringUtils;
import com.youfang.sixthmedia.chat.service.UNReadMessageMonitorService;
import com.youfang.sixthmedia.controller.HeaderController;
import com.youfang.sixthmedia.message.HousefilterResponse;
import com.youfang.sixthmedia.message.HousefilterResponse.Area;
import com.youfang.sixthmedia.message.HousefilterResponse.HouseType;
import com.youfang.sixthmedia.message.HousefilterResponse.Price;
import com.youfang.sixthmedia.message.model.City;
import com.youfang.sixthmedia.message.model.DetailBuilding;
import com.youfang.sixthmedia.message.model.User;
import com.youfang.sixthmedia.net.HttpRemoter;

public class YoufangContext {

	public static final int MESSAGE_POLL_INTERVAL = 2 * 1000;// used in
																// message
																// service...
	public static final int MAX_PHOTO_SIZE = 1024 * 1024 * 2;
	public static final int LIST_PAGE_SIZE = 20;
	public static final String CHAT_ATION = "com.sixmedia.chat.action";
	public static final String CHAT_UNREAD_MESSAGE_ATION = "com.sixmedia.chat.unread.action";

	public static final String APP_DIR = Environment
			.getExternalStorageDirectory().getAbsolutePath()
			+ File.separator
			+ "youfang" + File.separator;

	// //////////////////////////////////////
	private static String mycookie;

	public static String loadCookie() {

		HttpRemoter.mycookie = mycookie = getSharedPreferences().getString(
				"mycookie", null);
		return mycookie;

	}

	// ///////////////////////////////////////////
	public static boolean manualShown;

	public static boolean isManualShown() {

		SharedPreferences sp = getSharedPreferences();
		manualShown = sp.getBoolean("manualShown", false);

		return manualShown;

	}

	public static boolean saveManualShown() {
		Editor editor = prepare();
		editor.putBoolean("manualShown", true);
		return editor.commit();
	}

	// whether to show pic
	public static boolean showPic = true;

	public static void saveShowPic(boolean flag) {
		SharedPreferences.Editor editor = prepare();
		editor.putBoolean("show_pic", flag);
		editor.commit();
		showPic = flag;

	}

	public static void loadShowPic() {

		SharedPreferences sp = getSharedPreferences();
		showPic = sp.getBoolean("show_pic", true);

	}

	// ////////////////////////////////////////////

	// 登录的用户
	private static User user; // stored in db

	public static Uri getUserLogo() {
		if (user == null) {
			return null;
		}

		File file = new File(HeaderController.Cache.cachePath + user.userid);
		if (!file.exists()) {
			return null;

		}

		return Uri.fromFile(file);

	}

	public static User getUser() {
		return user;
	}

	public static boolean isLogined() {
		return user != null;

	}

	public static boolean saveUserAndStartMessageService(User user) {
		try {
			if (user.isPersisted()) {
				StoreManager.deleteAll(User.DUMY);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			user.store();
			YoufangContext.user = user;
			UNReadMessageMonitorService.start(AndroidUtils.appCtx());
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	public  static User loadUser() {

		try {
			List<User> users = StoreManager.findAll(User.DUMY);
			if (users.isEmpty()) {
				return null;
			}
			YoufangContext.user = users.get(0);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return user;
	}

	// //////////////////////////////////////////
	// 默认的城市
	public static City DEFAULT_CITY = new City();
	static {
		DEFAULT_CITY.cityid = 35;
		DEFAULT_CITY.name = "深圳";
	}

	// 当前的城市
	private static City curCity = DEFAULT_CITY;

	public static City loadCity() {
		String name = getSharedPreferences().getString("city_name", null);
		if (StringUtils.isBlank(name)) {
			curCity = DEFAULT_CITY;
			return curCity;
		}

		// 配置中存在该城市
		City city = new City();
		int id = getSharedPreferences().getInt("city_id", DEFAULT_CITY.cityid);
		city.cityid = id;
		city.name = name;
		curCity = city;

		return city;
	}

	public static City getCity() {
		return curCity;
	}

	public static boolean saveCity(City city) {
		SharedPreferences.Editor editor = prepare();
		String name = null;
		int id = DEFAULT_CITY.cityid;
		if (city != null) {
			name = city.name;
			id = city.cityid;
		}
		editor.putString("city_name", name);
		editor.putInt("city_id", id);
		if (editor.commit()) {
			curCity = city;
			return true;
		}
		return false;

	}

	// //////////////////////////////
	// keep some searcher param in context
	private static HousefilterResponse searcher_filter = new HousefilterResponse();
	public static Price searcher_curPrice = HousefilterResponse.PRICE;
	public static Area searcher_curArea = HousefilterResponse.AREA;
	public static HouseType searcher_curHT = HousefilterResponse.HT;
	public static String searcher_keyword = "";

	public static HousefilterResponse getSearcherFilter() {
		return searcher_filter;
	}

	public static void setSearcherFilter(HousefilterResponse resp) {
		searcher_filter = resp;
	}

	public static void resetSearcherFilter(String keyword) {
		searcher_curPrice = HousefilterResponse.PRICE;
		searcher_curArea = HousefilterResponse.AREA;
		searcher_curHT = HousefilterResponse.HT;
		searcher_keyword = keyword;
	}

	// ////////////////////

	// clear user data when user logout
	public synchronized static void clearUserInfo() {

		try {
			if (user != null && user.isPersisted()) {
				StoreManager.deleteAll(User.DUMY);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		user = null;
		try {
			HttpRemoter.clearCookie();
		} catch (Exception e) {
			// ignore...

		}

	}

	// 加载初始
	public static void init() {
		loadCity();
		loadUser();
		loadCookie();
		loadShowPic();
	}

	// ///获得广告批次/////////////////
	public static String getAdBatchID() {

		return getSharedPreferences().getString("adBatchID", "");
	}

	public static boolean setAdBatchID(String adBatchID) {
		SharedPreferences.Editor editor = prepare();
		editor.putString("adBatchID", adBatchID);
		return editor.commit();

	}

	public static Editor prepare() {
		return getSharedPreferences().edit();

	}

	public static SharedPreferences getSharedPreferences() {
		return AndroidUtils.appCtx().getSharedPreferences("Config",
				Context.MODE_PRIVATE);

	}

	// global current detail building...
	private static DetailBuilding detailBuilding;

	public static void setCurrentBuilding(DetailBuilding detail) {
		YoufangContext.detailBuilding = detail;
	}

	public static DetailBuilding getCurrentBuilding() {
		return detailBuilding;
	}

}
